var fetch = function (url, options) {
  wx.showLoading({title:'加载中',mask:true})
  let opt = options || {}, header
  if (url.indexOf('login') > 0) {
    header = {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  } else {
    if (wx.getStorageSync('token')) { 
      header = {
        'Content-Type': 'application/json;charset=UTF-8',
        'token': wx.getStorageSync('token'),
        // 'channel': 'mini'
      }
    }
  }
  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: opt.data,
      method: opt.type || 'get',
      header: opt.header ||header,
      success: function (res) {
        if (res.header.token) {
          wx.setStorageSync('token', res.header.token)
        }
        resolve(res.data)
      },
      fail: function (err) {
        reject(err);
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  })
}

export default fetch